﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using jSLApp.Client.Forms;
using jSLApp.Client.Workflow;
using jSLCore.Client.Presentation;
using jSLCore.Client.Presentation.ApplicationContainer;
using jSLCore.Client.Presentation.BaseControl;
using Telerik.Windows.Controls;

namespace jSLApp.Client.ApplicationBase
{
    public partial class MainContentContainer : ContentContainerBase
    {
        public override void ShowPage(BaseControl control, bool asDialog)
        {
            if (control != null)
            {
                base.ShowPage(control, asDialog);
                BindPage(control);
            }
        }

        private void BindPage(BaseControl control)
        {
            imgHeader.Header = control.Title;
            imgHeader.Image = control.Icon;
        }

        private void about_Clicked(object sender, RoutedEventArgs e)
        {
            var dlg = new SendMail();
            dlg.ShowDialog();
            NavigateTo(new About());
        }

        public MainContentContainer()
        {
            InitializeComponent();
        }

        protected override BaseControl GetDefaultPage()
        {
            return null;
        }

        protected override ContentPresenter GetMainContentPresenter()
        {
            return mainContent;
        }

        protected override ContentPresenter GetOkCancelControlContentPresenter()
        {
            return okCancelPresenter;
        }

        public static MainContentContainer Current
        {
            get { return (MainContentContainer)ApplicationContainer.CurrentContentContainer; }
        }

        public override void LoadInitialData()
        {
            var showCases = Helper.GetShowCases();
            var categories = showCases.GroupBy(s => s.Category).OrderBy(g => g.Key);
            foreach (var category in categories)
            {
                var categoryItem = new RadTreeViewItem();
                categoryItem.Header = category.Key;
                tree.Items.Add(categoryItem);

                var items = category.OrderBy(c => c.Order);
                foreach (var showCase in items)
                {
                    var showCaseItem = new RadTreeViewItem
                                           {
                                               Header = showCase.Name,
                                               Tag = showCase
                                           };
                    if (showCase.IsDefault)
                    {
                        showCaseItem.IsSelected = true;
                        categoryItem.IsExpanded = true;
                    }
                    categoryItem.Items.Add(showCaseItem);
                }
            }

            base.LoadInitialData();
        }

        private void sel_Changed(object sender, EventArgs e)
        {
            var tag = tree.SelectedContainer.Tag;
            if (tag != null)
            {
                var showcase = (ShowCase)tag;
                NavigateTo(showcase.CreateInstance());
            }
        }
    }
}
